const path = require('path')
const HtmlWebpaclPlugin = require('html-webpack-plugin')
const webpack = require('webpack')

/*
    输入
    输出
    编译: loader
    插件
*/

module.exports = {
    // mode: 'development',
    mode: 'development',
    // devtool: 'source-map',
    // devtool: 'source-map',
    devtool: 'cheap-source-map',
    entry: {
        main: path.resolve(__dirname, './src/index.js'),
    },
    devServer: {
        contentBase: path.resolve(__dirname, './build'),
        port: 3001,
        open: true,
        hot: true,
        hotOnly: true
    },
    output: {
        /*
            [name] 输入的 key
            [id] 文件索引
            [hash]
            [chunkhash]
        */
        filename: '[name]-[chunkhash:5]-bundle.js',
        path: path.resolve('./build'),
        // publicPath: '/'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: 'babel-loader'
            },
            // {
            //     test: /\.(png|jpe?g|gif)$/i,
            //     use: [
            //         {
            //             loader: 'file-loader',
            //         }
            //     ]
            // }
            {
                test: /\.(png|jpe?g|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 100 * 1024
                        }
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpaclPlugin({
            // title: "webpack-test",
            // filename: 'main.html'
            template: path.resolve(__dirname, './src/template/index.html')
        }),
        new webpack.HotModuleReplacementPlugin()
    ]
}
